package com.yanze.schoolinformation.view.home.news;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.example.xrecyclerview.XRecyclerView;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.adapter.rv.SchoolNewsAdapter;
import com.yanze.schoolinformation.base.BaseActivity;
import com.yanze.schoolinformation.databinding.ActivitySchoolNewsBinding;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.SchoolNewsBean;
import com.yanze.schoolinformation.viewModule.school_news.SchoolNewsNavigator;
import com.yanze.schoolinformation.viewModule.school_news.SchoolNewsViewModel;

import java.util.List;

public class SchoolNewsActivity extends BaseActivity<ActivitySchoolNewsBinding> implements SchoolNewsNavigator {
    private String mType;
    private SchoolNewsViewModel viewModel;
    private SchoolNewsAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_news);
        viewModel = new SchoolNewsViewModel(this);
        viewModel.setmNavigator(this);
        initData();
        setTitle(mType);

        initRecyclerView();

        viewModel.getSchoolNewsList();
    }

    public void initData() {
        mType = getIntent().getStringExtra("type");
        switch (mType) {
            case "通知公告":
                viewModel.setLoadUrl(C.url.notification);
                break;
            case "徐海新闻":
                viewModel.setLoadUrl(C.url.xuhainews);
                break;
            case "校园活动":
                viewModel.setLoadUrl(C.url.schoolactivity);
                break;
            case "学子之星":
                viewModel.setLoadUrl(C.url.studentstars);
                break;
            default:
                Toast.makeText(this, "数据错误，请刷新试试~", Toast.LENGTH_SHORT).show();
                finish();
                break;
        }
    }

    private void initRecyclerView() {
        mAdapter = new SchoolNewsAdapter();
        bindingView.xrvNews.setLayoutManager(new LinearLayoutManager(this));
        bindingView.xrvNews.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.setPage(1);
                viewModel.getSchoolNewsList();
            }

            @Override
            public void onLoadMore() {
                int page = viewModel.getPage();
                viewModel.setPage(++page);
                viewModel.getSchoolNewsList();
            }
        });
        bindingView.xrvNews.setAdapter(mAdapter);
    }

    @Override
    public void showSuccessView() {
        showContentView();
    }

    @Override
    public void setAdapterData(List<SchoolNewsBean> entipty) {
        mAdapter.clear();
        mAdapter.addAll(entipty);
        mAdapter.notifyDataSetChanged();
        bindingView.xrvNews.refreshComplete();
    }

    @Override
    public void setLoadMore(List<SchoolNewsBean> entipty) {
        mAdapter.addAll(entipty);
        mAdapter.notifyDataSetChanged();
        bindingView.xrvNews.refreshComplete();
    }

    @Override
    public void loadDataFaild() {
        bindingView.xrvNews.refreshComplete();
        if (mAdapter.getItemCount() == 0) {
            showError();
        }
    }

    @Override
    public void noMoreData() {
        bindingView.xrvNews.noMoreLoading();
    }

    @Override
    protected void onRefresh() {
        viewModel.getSchoolNewsList();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.onDestory();
    }

    /**
     * 入口
     */
    public static void start(Context context, String link) {
        Intent intent = new Intent(context, SchoolNewsActivity.class);
        intent.putExtra("type", link);
        context.startActivity(intent);
    }
}
